***STERILIZATION-ADJUSTED UNWANTED FAMILY PLANNING IN INDIA
***MAIN ANALYSIS
***David Canning and Mahesh Karra, 5-10-25

**# STEP 0: SETTING WORKING DIRECTORY
***To use the correct file directory, replace lines 11-12 below to your specific working directory where your files are stored.

version 13

if "`c(username)'"=="mvkarra" {
                global maindir1 "C:\Users\mvkarra\Dropbox\MET UNNEED STUDY\STERILIZATION IN INDIA PAPER" //Directory for where the pooled NFHS data is stored
				global maindir2 "C:\Users\mvkarra\Dropbox\MET UNNEED STUDY\STERILIZATION IN INDIA PAPER\RESULTS" //Directory for where results will be stored
}
else if "`c(username)'"=="" {
                global maindir1 ""
				global maindir1 ""
}

********************************************************************************
**# OPEN POOLED NFHS DATASET
use "$maindir1\REPLICATION_DATA.dta", clear

**# CHANGE DIRECTORY TO RESULTS
cd "$maindir2"

**# NSTALL ESTPOST AND ESTTAB COMMANDS (IF NOT ALREADY INSTALLED)
capture ssc install estout, replace
********************************************************************************


********************************************************************************
**# DATA PREPARATION
********************************************************************************
*****YEAR AND COUNTRY DUMMIES
tab year, gen(year)

gen country = substr(v000,1,2)
tab country, gen(country)

****SURVEY WEIGHTS
*Generate weight
gen weight = v005/1000000

****GENERATE UNIQUE CLUSTER VARIABLE
tostring v001, replace
gen uniq_cluster = v000 + " " + v001
destring v001, replace

****GENERATE UNIQUE HHID VARIABLE
tostring v002, replace
gen hh_id = uniq_cluster + " " + v002
destring v002, replace

*Make unique strata values by region/urban-rural (label option automatically labels the results)
egen strata = group(v024 v025), label

***GENERATE MOST RECENT SURVEY YEAR
bysort country: egen max_year = max(year)

*Set the weight (using pweights for robust standard errors), cluster (psu), and strata:
svyset [pweight=weight], psu(uniq_cluster) strata(strata) || hhid

*****ENCODE SURVEY VARIABLE FROM
encode v000, gen(survey)

*****UNIQUE WAVES
tab v000, gen(wave)
gen wave = 1
replace wave = 2 if wave2==1
replace wave = 3 if wave3==1

*****DROP OBSERVATIONS THAT ARE INCOMPLETE IN COVARIATES, DISTANCE MEASURES, OR OUTCOME VARIABLES
drop if weight==0
********************************************************************************

********************************************************************************
**# UNWANTED FAMILY PLANNING
********************************************************************************

********************************************************************************
*****SPECIFIC CATEGORIES - WHAT WOMEN WANT

***GENERATE WOMAN WANTS A CHILD SOON / NOW DUMMY
gen want_soon = v616==994
replace want_soon = . if v616==.

***GENERATE WOMAN WANTS A WITHIN 9 MONTHS (NOT INCLUDING SOON / NOW)
gen want_9_mths_num = v616 <= 109
replace want_9_mths_num = . if v616==.

***GENERATE WOMAN WANTS A CHILD LESS THAN NINE MONTHS / SOON / NOW DUMMY
gen want_9_mths = v616 <= 109 | v616==994
replace want_9_mths = . if v616==.

***GENERATE WOMAN WANTS A CHILD BETWEEN 10 MONTHS TO 2 YEARS
gen want_10_mths_2_yrs = (v616 > 109 & v616 <= 124) | v616==201 | v616==202
replace want_10_mths_2_yrs = . if v616==.

***GENERATE WOMAN WANTS A CHILD MORE THAN TWO YEARS FROM NOW OR WANT TO LIMIT
gen want_more_2_yrs_limit = (v616 > 124 & v616 <= 180) | (v616 > 202 & v616 <= 298) | v605==5 | v605==2
replace want_more_2_yrs_limit = . if v616==. | v605==.

***GENERATE WOMAN WANTS A CHILD MORE THAN TWO YEARS FROM NOW BUT DOES NOT WANT TO LIMIT
gen want_more_2_yrs = (v616 > 124 & v616 <= 180) | (v616 > 202 & v616 <= 298) | v605==2
replace want_more_2_yrs_limit = . if v616==. | v605==.

***GENERATE NOT KNOWN WHAT WOMAN WANTS
gen want_unknown = v616==199 | v616==299 | v616==992 | v616==993 | v616>=996 | v605==3 | v605==4 | v605==9
replace want_unknown = . if v616==. | v605==.

***GENERATE WOMEN CAN'T GET PREGNANT / STERILIZED / INFECUND / NEVER HAD SEX / AFTER MARRIAGE
gen want_cant_preg = v616==993 | v616==995 | v605==6 | v605==7 | v605==8 | v312==6 | v312==7
replace want_cant_preg = . if v616==. | v605==.

***GENERATE WOMEN WANTS AFTER MARRIAGE
gen want_after_marr = v616==993
replace want_after_marr = . if v616==. | v605==.

***GENERATE WOMEN CAN'T GET PREGNANT (INFECUND)
gen want_cant_preg_n = v616==995 | v605==7
replace want_cant_preg_n = . if v616==. | v605==.

***GENERATE WOMEN WANTS OTHER (NEVER HAD SEX, DESIRE FOR MORE, UNSURE TIMING)
gen want_other = v605==8 | v605==3 | v616==996
replace want_other = . if v616==. | v605==.

***GENERATE WOMEN DOES NOT KNOW / MISSING
gen want_dk_miss = v605==4 | v605==9 | v616==998 | v616==999 | v616==. | v605==.

***GENERATE WOMEN STERILIZED
gen wom_ster = v605==6 | v312==6 | v312==7
replace wom_ster = . if v605==. | v312==.

***GENERATE COMPOSITE VARIABLE OF WANTS
gen wom_wants = .
replace wom_wants = 1 if want_9_mths==1
replace wom_wants = 2 if want_10_mths_2_yrs==1
replace wom_wants = 3 if want_more_2_yrs_limit==1
replace wom_wants = 4 if want_unknown==1
replace wom_wants = 5 if want_cant_preg==1

label define womwants 1 "9 mths or <" 2 "10 mths - 2 yrs" 3 " > 2 yrs or limit" 4 "want but unk" 5 "other"
label values wom_wants womwants

***UPDATE WOMAN WANTS
gen wom_wants_n = .
replace wom_wants_n = 1 if want_soon==1
replace wom_wants_n = 2 if want_9_mths_num==1
replace wom_wants_n = 3 if want_10_mths_2_yrs==1
replace wom_wants_n = 4 if want_more_2_yrs==1
replace wom_wants_n = 5 if want_cant_preg_n==1
replace wom_wants_n = 6 if want_after_marr==1
replace wom_wants_n = 7 if want_other==1
replace wom_wants_n = 8 if want_dk_miss==1

label define womwants_n 1 "Soon / Now" 2 "9 mths or <" 3 "10 mths - 2 yrs" 4 " >= 2 yrs" 5 "Can't get pregnant" 6 "After marriage" 7 "Other" 8 "want but unk / miss"
label values wom_wants_n womwants_n

***RECODE WANTS
recode v605 (1/3 = 1 "Wants more")(5 = 2 "Wants no more")(7 8 = 3 "Other")(6 = 4 "Not asked")(4 9 . = 5 "Don't Know / Missing"), gen(wants_gen)

***GENERAL CATEGORIES: <= 2 YEARS, > 2 YEARS

***GENERATE WOMAN WANTS A CHILD LESS THAN OR EQUAL TO TWO YEARS FROM NOW
gen want_less_2_yrs = wom_wants==1 | wom_wants==2
replace want_less_2_yrs = . if wom_wants > 3
replace want_less_2_yrs = . if wom_wants==.

label define wantless 0 "want > 2 yrs or limit" 1 "want < 2 yrs"
label values want_less wantless

***GENERATE WOMAN WANTS A CHILD MORE THAN TWO YEARS FROM NOW OR WANT TO LIMIT
gen want_more_2_yrs_lim = v605==2 | v605==5
replace want_more_2_yrs_lim = . if v605==.

label define wantmore 0 "want < 2 yrs" 1 "want > 2 yrs or limit"
label values want_more_2_yrs_lim

***GENERATE DUMMY OF WANT NEXT CHILD: 9 MONTHS OR LESS = 1, 2+ YEARS = 2
gen wom_9_mths_2_yrs = .
replace wom_9_mths_2_yrs = 1 if want_9_mths==1
replace wom_9_mths_2_yrs = 2 if want_more_2_yrs_limit==1

label define wom_9_mths_2_yrs 1 "< 9 months" 2 "2+ years or limit"
label values wom_9_mths_2_yrs wom_9_mths_2_yrs

***GENERATE FERTILITY PREFERENCE VARIABLE
clonevar fert_pref = v602
replace fert_pref = 9 if v602 >= 8
replace fert_pref = . if v602==.
********************************************************************************


********************************************************************************
***GENERATE CONTRACEPTIVE USE DUMMY
gen cont_use = v313 > 0
replace cont_use = . if v313==.

label define contuse 0 "Not using" 1 "Using FP"
label values cont_use contuse

***GENERATE CONTRACEPTIVE USE CATEGORIES
recode v313 (0 = 0 "No Method")(1 2 = 1 "Traditional Methods")(3 = 2 "Modern Methods"), gen(cont_use_cat)
replace cont_use_cat = . if v313==.

********************************************************************************

***GENERATE UNMET NEED (V626)
gen unmet_need = v626==1 | v626==2
replace unmet_need = 1 if want_more_2_yrs_limit==1 & cont_use==0
replace unmet_need = . if v626==.

********************************************************************************

*******************************************
***UNWANTED FP (9 MTHS OR LESS / SOON / NOW
*******************************************

***GENERATE UNWANTED FP - 9 MONTHS OR LESS
gen uw_fp_9 = want_9_mths==1 & cont_use==1
replace uw_fp_9 = . if want_9_mths==. | cont_use==.

***************************************************************************
***POTENTIALLY UNWANTED FP (10-23 MTHS / NEVER ASKED / MISSING / STERILIZED
***************************************************************************

***GENERATE POTENTIALLY UNWANTED FP - 10 MONTHS TO 2 YEARS OR NEVER ASKED (STERILIZED)
gen pot_uw_fp_10 = (want_10_mths_2_yrs==1 | wom_ster==1) & cont_use==1
replace pot_uw_fp_10 = . if wom_ster==. | cont_use==.

********************************************************************************
**UPDATE UNWANTED FP CALCULUS TO EXCLUDE REGRET AND NEVER TOLD WOMEN
********************************************************************************

***DEFINITION 0: GENERATE UNWANTED FP - 9 MONTHS OR LESS, NOW INCLUDE STERILIZED WOMEN AS WANTED FP
gen uw_fp_9_new = want_9_mths==1 & cont_use==1
replace uw_fp_9_new = . if cont_use==.
replace uw_fp_9_new = 0 if wom_ster==1

*DEFINITION 1: WOMEN WHO REGRET STERILIZATION SHOULD BE CLASSIFIED AS UWFP
gen uw_fp_9_d1 = uw_fp_9_new
replace uw_fp_9_d1 = 0 if wom_ster==1
replace uw_fp_9_d1 = 1 if s340==1

*DEFINITION 2: WOMEN WHO DID NOT KNOW THAT STERILIZATION MEANT NOT HAVING MORE KIDS AS UWFP
gen uw_fp_9_d2 = uw_fp_9_d1
replace uw_fp_9_d2 = 1 if v3a01==0


********************************************************************************
**# TABLES****
********************************************************************************

***TABLE 1 AND APPENDIX TABLE 1: 
***METHODOLOGY REPLICATED FROM CANNING AND KARRA (2023): "Unwanted Family Planning: Prevalence Estimates for 56 Countries" DOI: 10.1111/sifp.12230

***TABLE 2 AND APPENDIX TABLE 2:

***TABULATION OF WOMEN WHO ARE STERILIZED IN INDIA OVER DHS SURVEYS
estpost svy: tab wom_ster wave, col
esttab using IA_TABLE_2.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABLE 3 AND APPENDIX TABLE 3:

***TABULATION OF WOMEN WHO REGRET BEING STERILIZED IN INDIA OVER DHS SURVEYS
estpost svy: tab s340 wave, col
esttab using IA_TABLE_3A.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace
***TABULATION OF WOMEN WHO DID NOT KNOW ABOUT KIDS WHEN STERILIZED IN INDIA OVER DHS SURVEYS
estpost svy: tab v3a01 wave, col
esttab using IA_TABLE_3B.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABLE 4:

***CROSS-TABULATION OF WOMEN WHO REGRET AND DID NOT KNOW ABOUT KIDS WHEN STERILIZED IN INDIA, 2019-21
estpost svy: tab s340 v3a01 if wave3==1, cell
esttab using IA_TABLE_4_2021.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***APPENDIX TABLE 4:

***CROSS-TABULATION OF WOMEN WHO REGRET AND DID NOT KNOW ABOUT KIDS WHEN STERILIZED IN INDIA, 2005-06
estpost svy: tab s340 v3a01 if wave1==1, cell
esttab using IA_TABLE_4_2006.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***CROSS-TABULATION OF WOMEN WHO REGRET AND DID NOT KNOW ABOUT KIDS WHEN STERILIZED IN INDIA, 2015-16
estpost svy: tab s340 v3a01 if wave2==1, cell
esttab using IA_TABLE_4_2016.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABLE 5 AND APPENDIX TABLE 5:

***TABULATION OF UWFP (ORIG.) OVER TIME
estpost svy: tab uw_fp_9 wave, col
esttab using IA_TABLE_5A.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABULATION OF UWFP (DEF. 1) OVER TIME
estpost svy: tab uw_fp_9_d1 wave, col
esttab using IA_TABLE_5B.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABULATION OF UWFP (DEF. 2) OVER TIME
estpost svy: tab uw_fp_9_d2 wave, col
esttab using IA_TABLE_5C.rtf, cells("b(fmt(3)) obs(fmt(0))") varlabels(`e(labels)') nonumber unstack nomtitle label replace

***TABLE 6: STATE SPECIFIC ESTIMATES

***GENERATE TABLE OF MEAN UFP AND STERILIZATION-ADJUSTED UFP VALUES BY STATE
foreach x of numlist 1/25 27/37 {
preserve
keep if v024==`x' & wave3==1
collapse (mean) v024 uw_fp_9_new uw_fp_9_d1 uw_fp_9_d2 [pweight=weight], by(v000)
save "$maindir1\CLEAN DATA\MEAN_UN_DATA_`x'.dta", replace
restore
}
***COMBINE STATE ESTIMATES INTO ONE DATASET, TABLE 6
preserve
use "$maindir1\CLEAN DATA\MEAN_UN_DATA_1.dta", clear
foreach x of numlist 2/25 27/37 {
append using "$maindir1\CLEAN DATA\MEAN_UN_DATA_`x'.dta"
}
save "$maindir1\CLEAN DATA\MEAN_UN_DATA_STATE.dta", replace
restore
